package com.xtgg.history;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapStatusChangeListener;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.loopj.android.http.RequestParams;
import com.umeng.analytics.MobclickAgent;
import com.xtgg.R;
import com.xtgg.manager.BaseActivity;
import com.xtgg.model.Resp;
import com.xtgg.sys.Constants;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;
import com.xtgg.util.PreferencesUtils;
import com.xtgg.util.TLog;
import com.xtgg.util.ToastUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

public class MapLocationActivity extends BaseActivity implements OnClickListener {
	MapView mMapView = null;
	BaiduMap mBaiduMap;
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map_location);

		((TextView) findViewById(R.id.center)).setText("足迹");
		MobclickAgent.onEvent(this, ((TextView) findViewById(R.id.center))
				.getText().toString());

		((Button) findViewById(R.id.right)).setText("分享");
		((Button) findViewById(R.id.right)).setTextColor(Color
				.parseColor("#EE7C1C"));
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);

		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		mBaiduMap.setMyLocationEnabled(true);
		
		MyLocationData locData = new MyLocationData.Builder()
		.accuracy(1000).direction(100)
		.latitude(((DemoApp)this.getApplicationContext()).mLat)
		.longitude(((DemoApp)this.getApplicationContext()).mLng).build();
		mBaiduMap.setMyLocationData(locData);
		
		LatLng ll = new LatLng(getIntent().getExtras().getDouble("lat"),
				getIntent().getExtras().getDouble("lng"));
		MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll,16);
		mBaiduMap.animateMapStatus(u);
		
		LatLng point = new LatLng(getIntent().getExtras().getDouble("lat"),
				getIntent().getExtras().getDouble("lng"));  
		BitmapDescriptor bitmap = BitmapDescriptorFactory  
		    .fromResource(R.drawable.ic_loc);  
		//构建MarkerOption，用于在地图上添加Marker  
		OverlayOptions option = new MarkerOptions()  
		    .position(point)  
		    .icon(bitmap).title(getIntent().getStringExtra("addr"));  
		mBaiduMap.addOverlay(option);
		
		((TextView) findViewById(R.id.addr)).setText(getIntent().getStringExtra("addr"));
	}
	
	boolean hasDraw;
	Dialog mDialog = null;
	void showQuery() {
		mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		mDialog.setContentView(R.layout.dialog_share);
		mDialog.findViewById(R.id.action0).setOnClickListener(this);
		mDialog.findViewById(R.id.action1).setOnClickListener(this);
		mDialog.findViewById(R.id.action2).setOnClickListener(this);
		mDialog.findViewById(R.id.action3).setOnClickListener(this);
		mDialog.findViewById(R.id.login).setOnClickListener(this);
		mDialog.show();
	}
	
	 public Bitmap convertViewToBitmap(View view) {
		 WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		 int width = wm.getDefaultDisplay().getWidth();
		 int height = wm.getDefaultDisplay().getHeight();
		 
		 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
		 view.draw(new Canvas(b));
		 return b;
	 }

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.right:
			showQuery();
			break;
			
		case R.id.left:
			finish();
			break;

		case R.id.action0:
		{
			HistorySumActivity.mScreenShot = convertViewToBitmap(findViewById(R.id.screen));
			
			Intent intent = new Intent(this,ShareActivity.class);
			intent.putExtra("type", 0);
			intent.putExtra("content", "小兔乖乖足迹");
			startActivity(intent);
		}
			mDialog.dismiss();
			break;
	
		case R.id.action1:
		{
			HistorySumActivity.mScreenShot = convertViewToBitmap(findViewById(R.id.screen));
			
			Intent intent = new Intent(this,ShareActivity.class);
			intent.putExtra("type", 1);
			intent.putExtra("content", "小兔乖乖足迹");
			startActivity(intent);
		}
			mDialog.dismiss();
			break;
	
		case R.id.action2:
		{
			HistorySumActivity.mScreenShot = convertViewToBitmap(findViewById(R.id.screen));
			
			Intent intent = new Intent(this,ShareActivity.class);
			intent.putExtra("type", 2);
			intent.putExtra("content", "小兔乖乖足迹");
			startActivity(intent);
		}
			mDialog.dismiss();
			break;
	
		case R.id.action3:
		{
			HistorySumActivity.mScreenShot = convertViewToBitmap(findViewById(R.id.screen));
			
			Intent intent = new Intent(this,ShareActivity.class);
			intent.putExtra("type", 3);
			intent.putExtra("content", "小兔乖乖足迹");
			startActivity(intent);
		}
			mDialog.dismiss();
			break;
		}
	}
}
